/**
 * 	 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *   @author Colin Redmond
 *   Copyright (C) 2011 Colin Redmond
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.cred.industries.platform.business.objects;

import java.util.ArrayList;
import java.util.List;

import com.cred.industries.platform.business.security.Authorization;
import com.cred.industries.platform.defines.Role;
import com.cred.industries.platform.session.SessionData;

/**
 * Contains all the roles the customerId has
 */
public class CustomerRolesBO implements Authorization {

	private List<Role> mRoles = new ArrayList<>();
	private String mCustomerPersonaName;
	
	/**
	 * constructor
	 * @param customerId
	 */
	public CustomerRolesBO(String customerPersonaName) {
		super();
		this.mCustomerPersonaName = customerPersonaName;
	}

	/**
	 * returns true if the customerId has a role
	 * @param role the role we are checking to see if we have
	 * @return true if the customerId has the role asked 
	 */
	public boolean hasRole(Role role) {
		return mRoles.contains(role);
	}
	
	/**
	 * assigns a new role to the customerId
	 * @param role to give
	 */
	public void assignRole(Role role) {
		mRoles.add(role);
	}
	
	/**
	 * gets all roles a customerId has
	 * @return all roles in the BO
	 */
	public List<Role> getRoles() {
		return mRoles;
	}
	
	/**
	 * Gets the highest role this customerId has. 
	 * The order is defined in the Role enum
	 * @return the highest role this customerId has. 
	 */
	public Role getHighestRole() {
		
		Role highest = mRoles.get(0);
		for(Role r : mRoles) {
			highest = r.getRolePriority() < highest.getRolePriority() ? r : highest;
		}
		return highest;
	}

	public String getCustomerPersonaName() { return mCustomerPersonaName; } 

	/**
	 * returns if the currently authenticated customer is authorized to modify
	 * Only an admin can change roles
	 * @return true if the currently authenticated customer is an admin
	 */
	@Override
	public boolean authorizedToModify() {

		//only an admin may alter this BO
		CustomerBO authenticatedUser = SessionData.getSessionCustomer();
		CustomerRolesBO roles = authenticatedUser.getRoles();
		return authenticatedUser != null && roles.hasRole(Role.ADMIN);
	}
	
	/**
	 * anyone can access this BO
	 * @return true
	 */
	@Override
	public boolean authorizedToAccess() {
		
		//any one can read this BO
		return true;
	}

	/**
	 * returns if the currently authenticated customer is the Super user
	 * Only an admin can change roles
	 * @return true if the currently authenticated customer is an admin
	 */
	@Override
	public boolean authorizedAsSuperuser() {

		//only an admin may alter this BO
		CustomerBO authenticatedUser = SessionData.getSessionCustomer();
		CustomerRolesBO roles = authenticatedUser.getRoles();
		return authenticatedUser != null && roles.hasRole(Role.ADMIN);
	}
}
